module.exports = class teacher_mod extends require('./model') {
  // 获取请假条
  static getLeaveMod(classArr, currPage, pageSize) {
    let startIndex = (currPage - 1) * pageSize
    pageSize -= 0;

    return new Promise((resolve, reject) => {
      let sql = "select l.*, u.username from `leave` l ";
      sql += " join `user` u on u.id = l.u_id where l.classes = "
      let length = classArr.length
      for (let i = 0; i < length; i++) {
        if (i == 0) {
          sql += `"${classArr[i]}"`
        } else {
          sql += ` or l.classes = "${classArr[i]}"`
        }
      }
      sql += " order by create_time desc limit" + ` ${startIndex},${pageSize}`
      this.query(sql).then(res => {
        resolve(res)
      }).catch(err => {
        reject("您还没有学生请假~~")
      })
    })
  }

  //获取请假总数
  static getLeaveTotal(classArr) {
    return new Promise((resolve, reject) => {
      let sql = "select count(1) count from `leave` where classes = ";
      let length = classArr.length
      for (let i = 0; i < length; i++) {
        if (i == 0) {
          sql += `"${classArr[i]}"`
        } else {
          sql += ` or classes = "${classArr[i]}"`
        }
      }
      sql += ` order by create_time desc`
      this.query(sql).then(res => {
        resolve(res)
      }).catch(err => {
        reject("您还没有学生请假~~")
      })
    })
  }


  // 获取用户请假条
  static getUserLeaveMod(u_id, currPage, pageSize) {
    let startIndex = (currPage - 1) * pageSize
    pageSize -= 0;

    return new Promise((resolve, reject) => {
      let sql = "select * from `leave` where u_id = ? order by create_time desc limit ?,?";
      this.query(sql, [u_id, startIndex, pageSize]).then(res => {
        resolve(res)
      }).catch(err => {
        reject("您还没有请假~~")
      })
    })
  }

  //获取用户请假总数
  static getUserLeaveTotal(u_id) {
    return new Promise((resolve, reject) => {
      let sql = "select count(1) count from `leave` where u_id = ?";
      this.query(sql, [u_id]).then(res => {
        resolve(res)
      }).catch(err => {
        reject("您还没有请假~~")
      })
    })
  }

  // 当前请假单状态审批
  static upLeaveStateMod(l_id, upState) {
    return new Promise((resolve, reject) => {
      l_id -= 0;
      upState -= 0;
      let sql = "update `leave` set state = ? where l_id = ?"
      this.query(sql, [upState, l_id]).then(res => {
        resolve("审批完成")
      }).catch(err => {
        reject("审批失败")
      })
    })
  }

  //获取班级名称
  static getCidByCnameMod(cname) {
    return new Promise((resolve, reject) => {
      let sql = "select *  from `classes` where class = ?";
      this.query(sql, [cname]).then(res => {
        resolve(res)
      }).catch(err => {
        reject("没有该班级")
      })
    })
  }

  // 按页获得风险人群 
  static getRiskedPeoplePageMod(begin, end, pageSize, currPage) {
    let startIndex = (currPage - 1) * pageSize;
    pageSize -= 0;
    return new Promise((resolve, reject) => {
      let sql = "select u.*,h.temperature,h.fever from `user` u join `health` h on h.u_id = u.id and (h.fever = '是' || h.temperature >= 37.2) and h.create_time between ? and ? limit ?,?";
      this.query(sql, [begin, end, startIndex, pageSize]).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  }

  // 获取风险人群总数 
  static getRiskedPeopleTotal(begin, end) {
    return new Promise((resolve, reject) => {
      let sql = "select count(1) count from `user` u join `health` h on h.u_id = u.id and (h.fever = '是' || h.temperature >= 37.2) and h.create_time between ? and ?";
      this.query(sql, [begin, end]).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  }

  // 按页获得经过高风险地区人群 
  static getDangerPeoplePageMod(begin, end, pageSize, currPage) {
    let startIndex = (currPage - 1) * pageSize;
    pageSize -= 0;
    return new Promise((resolve, reject) => {
      let sql = "select u.* from `user` u join `health` h on h.u_id = u.id and godanger = '是' and h.create_time between ? and ? limit ?,?";
      this.query(sql, [begin, end, startIndex, pageSize]).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  }

  // 获取经过高风险地区人群总数 
  static getDangerPeoplePageTotal(begin, end) {
    return new Promise((resolve, reject) => {
      let sql = "select count(1) count from `user` u join `health` h on h.u_id = u.id and godanger = '是' and h.create_time between ? and ?";
      this.query(sql, [begin, end]).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  }

}